const State = {
    list: JSON.parse(localStorage.getItem('todo')) || [],
    listAll: JSON.parse(localStorage.getItem('todo')) || [],
}
export function todo(state = State, action) {
    console.log(action);
    switch (action.type) {
        case 'add':
            var flag = false
            state.list.forEach(item => {
                if (item.value === action.value) {
                    flag = true
                }
            })
            if (flag) {
                alert('内容不能重复')
                return state;
            }
            state.list.push(action)
            state.listAll.push(action)
            localStorage.setItem('todo', JSON.stringify(state.list));
            return {
                list: [...state.list],
                listAll: [...state.listAll],
            };
        case 'all':
            localStorage.setItem('todo', JSON.stringify(state.listAll));
            return {
                list: [...state.listAll],
                listAll: [...state.listAll],
            }
        case 'no':
            state.list = []
            state.listAll.forEach(item => {
                if (item.status === 0) {
                    state.list.push(item)
                }
            })
            localStorage.setItem('todo', JSON.stringify(state.list));
            return {
                list: [...state.list],
                listAll: [...state.listAll],
            }
        case 'yes':
            state.list = []
            state.listAll.forEach(item => {
                if (item.status === 1) {
                    state.list.push(item)
                }
            })
            localStorage.setItem('todo', JSON.stringify(state.list));
            return {
                list: [...state.list],
                listAll: [...state.listAll],
            }
        case 'change':
            state.listAll.forEach(ele => {
                if (ele.value === action.item.value) {
                    if (action.item.status === 0) {
                        ele.status = 1
                    } else {
                        ele.status = 0
                    }
                }
            })
            state.list = [...state.listAll]
            localStorage.setItem('todo', JSON.stringify(state.list));
            return {
                list: [...state.list],
                listAll: [...state.listAll]
            }
        case 'del':
            state.listAll.forEach((ele, index) => {
                if (ele.value === action.item.value) {
                    state.listAll.splice(index, 1)
                }
            })
            state.list = [...state.listAll]
            localStorage.setItem('todo', JSON.stringify(state.list));
            return {
                list: [...state.list],
                listAll: [...state.listAll]
            }
        case 'changeAll':
            state.listAll.forEach(item => {
                item.status = 1
            })
            state.list = [...state.listAll]
            localStorage.setItem('todo', JSON.stringify(state.list));
            return {
                list: [...state.list],
                listAll: [...state.listAll]
            }
        case 'remove':
            state.listAll = [];
            state.list = [...state.listAll]
            localStorage.setItem('todo', JSON.stringify(state.list));
            return {
                list: [...state.list],
                listAll: [...state.listAll]
            }
        default:
            return state;
    }
}